package com.fjec.common.convert;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.core.MethodParameter;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

public class PaginationParamConvert implements HandlerMethodArgumentResolver {

    private String pageParameterName = "page";

    private String limitParameterName = "limit";

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return Page.class == parameter.getParameterType();
    }

    @Override
    public Object resolveArgument(MethodParameter parameter,
                                  ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
                                  WebDataBinderFactory binderFactory) throws Exception {
        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();

        int page = ServletRequestUtils.getRequiredIntParameter(request, pageParameterName);
        int limit = ServletRequestUtils.getRequiredIntParameter(request, limitParameterName);
        Page paginationParam = new Page(page, limit);

        return paginationParam;
    }

    public void setPageParameterName(String pageParameterName) {
        Assert.notNull(pageParameterName, "pageParameterName cannot be null");
        this.pageParameterName = pageParameterName;
    }

    public void setLimitParameterName(String limitParameterName) {
        Assert.notNull(limitParameterName, "limitParameterName cannot be null");
        this.limitParameterName = limitParameterName;
    }
}
